// MainWindow.cpp : implementation file
//

#include "stdafx.h"
#include "eyekeeper.h"
#include "MainWindow.h"
#include "resource.h"
#include <math.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMainWindow

CMainWindow::CMainWindow()
{
	d=0;
	an=0;
	nMethod=0;
	frame=0;
	dx=1;
}

CMainWindow::~CMainWindow()
{
}


BEGIN_MESSAGE_MAP(CMainWindow, CWnd)
	ON_WM_CONTEXTMENU()
	//{{AFX_MSG_MAP(CMainWindow)
	ON_WM_CREATE()
	ON_WM_TIMER()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_MOUSEMOVE()
	ON_WM_MOUSEWHEEL()
	ON_WM_LBUTTONDBLCLK()
	ON_WM_MOVING()
	ON_WM_MOVE()
	ON_WM_DROPFILES()
	ON_WM_DESTROY()
	ON_COMMAND(ID_MENUITEM_EXIT, OnMenuitemExit)
	ON_COMMAND(ID_MENUITEM_METHOD1, OnMenuitemMethod1)
	ON_COMMAND(ID_MENUITEM_METHOD2, OnMenuitemMethod2)
	ON_COMMAND(ID_MENUITEM_METHOD3, OnMenuitemMethod3)
	ON_WM_NCDESTROY()
	ON_COMMAND(ID_MENUITEM_METHOD4, OnMenuitemMethod4)
	ON_COMMAND(ID_MENUITEM_ABOUT, OnMenuitemAbout)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// CMainWindow message handlers



int CMainWindow::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// TODO: Add your specialized creation code here
	/*HMODULE hUser32 = GetModuleHandle(_T("USER32.DLL"));
	HMODULE hGdi32 = GetModuleHandle(_T("GDI32.DLL"));
	UpdateLayeredWindow = (lpfnUpdateLayeredWindow)GetProcAddress(hUser32, "UpdateLayeredWindow");
	SetLayeredWindowAttributes = (lpfnSetLayeredWindowAttributes)GetProcAddress(hUser32, "SetLayeredWindowAttributes");
	AlphaBlend2 = (lpfnAlphaBlend)GetProcAddress(hGdi32, "GdiAlphaBlend");*/
	
	//ASSERT(UpdateLayeredWindow);
	/*if (NULL == UpdateLayeredWindow)
	{
		::MessageBox(NULL,"Not Found Layered Window Support","Error",MB_OK);
		return -1;
	}
	if (NULL == SetLayeredWindowAttributes)
	{
		::MessageBox(NULL,"Not Found Layered Window Support","Error",MB_OK);
		return -1;
	}
	if (NULL == AlphaBlend2)
	{
		::MessageBox(NULL,"Not Found Alpha Blend Support","Error",MB_OK);
		return -1;
	}*/
	if (FALSE == OpenAVI("head_Blend.avi","head_Alpha.avi"))
	{
		::MessageBox(NULL,"OpenAVI Error","Error",MB_OK);
		return -1;
	}
	bMoveingWindow=false;
	Blend.BlendOp = AC_SRC_OVER;
	Blend.BlendFlags = 0;
	Blend.AlphaFormat = AC_SRC_ALPHA;
	Blend.SourceConstantAlpha = 0xff;
	Size.cx=128;
	Size.cy=128;
	ptDst.x=0;
	ptDst.y=0;
	ptSrc.x=0;
	ptSrc.y=0;
	nFps=30;
	nTimeCount=0;
	hScreenDC=::GetDC(NULL);
	hBackBufferDC=CreateCompatibleDC(NULL);
	BITMAPINFO pbmiRGB;
	pbmiRGB.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
	pbmiRGB.bmiHeader.biWidth= Size.cx;
	pbmiRGB.bmiHeader.biHeight= Size.cy;
	pbmiRGB.bmiHeader.biPlanes= 1;
	pbmiRGB.bmiHeader.biBitCount      = 32;
	pbmiRGB.bmiHeader.biCompression   = BI_RGB;
	pbmiRGB.bmiHeader.biSizeImage     = pbmiRGB.bmiHeader.biWidth* abs(pbmiRGB.bmiHeader.biHeight) * 4;
	hBackBufferBitmap = CreateDIBSection(hBackBufferDC, &pbmiRGB, DIB_RGB_COLORS,(PVOID *) &pBits, NULL, 0);

	if (!hBackBufferBitmap)
	{
		::MessageBox(NULL, _T("CreateDIBSection"), _T("Error"), MB_OK);
		return -1;
	}    
	hOldBitmap=(HBITMAP)SelectObject(hBackBufferDC, hBackBufferBitmap);
	if (hOldBitmap==NULL)
	{
		::MessageBox(NULL, _T("SelectObject(hBackBufferDC, hBackBufferBitmap)"), _T("Error"), MB_OK);
		return -1;
	} 
	int i,j,k;
	for(i=0;i<Size.cy;i++)
	{
		for(j=0;j<Size.cx;j++)
		{
			*(pBits+(i*Size.cx+j)*4)=0x0;
			*(pBits+(i*Size.cx+j)*4+1)=0x0;
			*(pBits+(i*Size.cx+j)*4+2)=0x0;
			*(pBits+(i*Size.cx+j)*4+3)=0x0;
		}
	}
	::GetSystemTime(&stStartTime);
	::UpdateLayeredWindow(this->m_hWnd,hScreenDC,&ptDst,&Size,hBackBufferDC, &ptSrc,RGB(0xff,0xff,0x0),&Blend,ULW_ALPHA);
	SetTimer(IDT_REFRESH,1000/nFps,NULL);
	XFORM xForm;
	::SetGraphicsMode(hBackBufferDC, GM_ADVANCED);
	//::SetMapMode(hBackBufferDC, MM_ISOTROPIC); 
	::SetWindowOrgEx(hBackBufferDC,-64,-64,NULL);
	::SetViewportExtEx(hBackBufferDC,2,2,NULL);
	//::SetMapMode(hBackBufferDC, MM_ISOTROPIC); 
	xForm.eM11 = (FLOAT) 0.8660; 
    xForm.eM12 = (FLOAT) 0.5000; 
    xForm.eM21 = (FLOAT) -0.5000; 
    xForm.eM22 = (FLOAT) 0.8660; 
    xForm.eDx  = (FLOAT) 0.0; 
    xForm.eDy  = (FLOAT) 0.0; 
    //::SetWorldTransform(hBackBufferDC, &xForm); 
	FILE * bf;
	bf=fopen("glass.raw","rb");
	pAlpha=new unsigned char[128*128*4];
	fread(pAlpha,128*128*4,1,bf);
	fclose(bf);
	BYTE t;
	unsigned int a;
	for(i=0;i<Size.cy;i++)
	{
		for(j=0;j<Size.cx;j++)
		{
			t=*(pAlpha+(i*Size.cx+j)*4);
			*(pAlpha+(i*Size.cx+j)*4)=*(pAlpha+(i*Size.cx+j)*4+2);
			*(pAlpha+(i*Size.cx+j)*4+2)=t;
			/*for(k=0;k<3;k++)
			{
				a=*(pAlpha+(i*Size.cx+j)*4+k)**(pAlpha+(i*Size.cx+j)*4+3)/0xff;
				if(a>0xff)
				{a=0xff;}
				*(pAlpha+(i*Size.cx+j)*4+k)=a;
				//*(pBits+(i*Size.cx+j)*4+3)=0x0;
			}*/
		}
	}
	return 0;
}

BOOL CMainWindow::PreCreateWindow(CREATESTRUCT& cs) 
{
	// TODO: Add your specialized code here and/or call the base class
	return CWnd::PreCreateWindow(cs);
}

void CMainWindow::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	int i,j,k;
	//::BitBlt(hBackBufferDC,-64,-64,64,64,hScreenDC,0,0,SRCCOPY);
	pTemp=pBits;
	unsigned char* pa=pAlpha;
	::GetSystemTime(&stCurrentTime);
	XFORM xForm;
	xForm.eM11 = (FLOAT) cos(nTimeCount/4.0); 
    xForm.eM12 = (FLOAT) sin(nTimeCount/4.0); 
    xForm.eM21 = (FLOAT) -sin(nTimeCount/4.0); 
    xForm.eM22 = (FLOAT) cos(nTimeCount/4.0); 
	xForm.eDx  = (FLOAT) 0.0; 
    xForm.eDy  = (FLOAT) 0.0; 
	if(d)
	{
		an+=0.5*d/120.0;
		d/=2;
	}
	/*xForm.eM11 = (FLOAT) cos(an); 
    xForm.eM12 = (FLOAT) sin(an); 
    xForm.eM21 = (FLOAT) -sin(an); 
    xForm.eM22 = (FLOAT) cos(an); 
    xForm.eDx  = (FLOAT) 0.0; 
    xForm.eDy  = (FLOAT) 0.0; */
    ::SetWorldTransform(hBackBufferDC, &xForm);
	if(nMethod==0)
	{
		for(int k=0;k<128*128*4;k++)
		{
			*pTemp=0;
			pTemp++;
		}
		DrawRadiationAlarm(1);
	}
	else if(nMethod==1)
	{
		for(int k=0;k<128*128*4;k++)
		{
			*pTemp=0;
			pTemp++;
		}
		DrawAlarmLight(1,0.2);
	}
	else if(nMethod==2)
	{
		GrabAVIFrame(frame);
	}
	else if(nMethod==3)
	{
		for(int k=0;k<128*128*4;k++)
		{
			*pTemp=*pa;
			pTemp++;
			pa++;
		}
		/*unsigned int a;
		for(i=0;i<Size.cy;i++)
		{
			for(j=0;j<Size.cx;j++)
			{
				for(k=0;k<3;k++)
				{
					a=*(pBits+(i*Size.cx+j)*4+k)**(pBits+(i*Size.cx+j)*4+3)/0xff;
					if(a>0xff)
					{a=0xff;}
					*(pBits+(i*Size.cx+j)*4+k)=a;
					//*(pBits+(i*Size.cx+j)*4+3)=0x0;
				}
			}
		}*/
	}
	else
	{}
	/*unsigned int a,b;
		for(i=0;i<Size.cy;i++)
		{
			for(j=0;j<Size.cx;j++)
			{
				for(k=0;k<3;k++)
				{
					a=*(pBits+(i*Size.cx+j)*4+k);
					b=*(pBits+(i*Size.cx+j)*4+3);
					if(b==0) break;
					a=a*0xff/b;
					if(a>0xff)
					{a=0xff;}
					*(pBits+(i*Size.cx+j)*4+k)=a;
					//*(pBits+(i*Size.cx+j)*4+3)=0x0;
				}
			}
		}*/
	
	//::BitBlt(hBackBufferDC,-64,-64,64,64,hScreenDC,0,0,SRCCOPY);
	::UpdateLayeredWindow(this->m_hWnd,hScreenDC,&ptDst,&Size,hBackBufferDC, &ptSrc,RGB(0xff,0xff,0x0),&Blend,ULW_ALPHA);
	CWnd::OnTimer(nIDEvent);
	nTimeCount++;
	frame+=dx;
	if (frame==lastframe-1)										// Are We At Or Past The Last Frame?
	{
		dx=-1;												// Reset The Frame Back To Zero (Start Of Video)
		//frame=lastframe-1;
	}
	if (frame==0)										// Are We At Or Past The Last Frame?
	{
		dx=1;												// Reset The Frame Back To Zero (Start Of Video)
		//frame=0;
	}
	/*frame++;
	if (frame>=lastframe)										// Are We At Or Past The Last Frame?
	{
		frame=0;
	}*/
	//nMethod=5;
}

void CMainWindow::DrawRadiationAlarm(int angle)
{
	TRIVERTEX        vert [3] ;
	GRADIENT_TRIANGLE    gTri[1];
	int step=10;
	int v;
	int halfwidth=30;
	int step2=10;
	float r=64,r0=12,r1=16,r2=50;
	float u;
	float c=(Size.cx-1)/2.0;
	gTri[0].Vertex1   = 0;
	gTri[0].Vertex2   = 1;
	gTri[0].Vertex3   = 2;
	for(v=step;v<=360;v+=step)
	{
		u=v*3.14159265358/180;
		vert [0] .x       =  0;
		vert [0] .y       =  0;
		vert [0] .Red     =  0xff00;
		vert [0] .Green   =  0xff00;
		vert [0] .Blue    =  0x0000;
		vert [0] .Alpha   =  0x7f00;

		vert [1] .x       =  cos(u)*r;
		vert [1] .y       =  sin(u)*r;
		vert [1] .Red     =  0x00;
		vert [1] .Green   =  0x00;
		vert [1] .Blue    =  0x00;
		vert [1] .Alpha   =  0x00;

		u=(v-step)*3.14159265358/180;
		vert [2] .x       =  cos(u)*r;
		vert [2] .y       =  sin(u)*r;
		vert [2] .Red     =  0x00;
		vert [2] .Green   =  0x00;
		vert [2] .Blue    =  0x00;
		vert [2] .Alpha   =  0x00;
		GradientFill(hBackBufferDC,vert,3,&gTri,1,GRADIENT_FILL_TRIANGLE);
	}
	for(int i=0;i<3;i++)
	{
		for(v=angle-halfwidth+step2;v<=angle+halfwidth;v+=step2)
		{
			u=v*3.14159265358/180;
			vert [0] .x       =  cos(u)*r1;
			vert [0] .y       =  sin(u)*r1;
			vert [0] .Red     =  0x000;
			vert [0] .Green   =  0x000;
			vert [0] .Blue    =  0x000;
			vert [0] .Alpha   =  0xff00;

			vert [1] .x       =  cos(u)*r2;
			vert [1] .y       =  sin(u)*r2;
			vert [1] .Red     =  0x00;
			vert [1] .Green   =  0x00;
			vert [1] .Blue    =  0x00;
			vert [1] .Alpha   =  0xff00;

			u=(v-step2)*3.14159265358/180;
			vert [2] .x       =  cos(u)*r2;
			vert [2] .y       =  sin(u)*r2;
			vert [2] .Red     =  0x00;
			vert [2] .Green   =  0x00;
			vert [2] .Blue    =  0x00;
			vert [2] .Alpha   =  0xff00;
			GradientFill(hBackBufferDC,vert,3,&gTri,1,GRADIENT_FILL_TRIANGLE);
			u=(v-step2)*3.14159265358/180;
			vert [0] .x       =  cos(u)*r1;
			vert [0] .y       =  sin(u)*r1;
			vert [0] .Red     =  0x000;
			vert [0] .Green   =  0x000;
			vert [0] .Blue    =  0x000;
			vert [0] .Alpha   =  0xff00;

			u=v*3.14159265358/180;
			vert [1] .x       =  cos(u)*r1;
			vert [1] .y       =  sin(u)*r1;
			vert [1] .Red     =  0x00;
			vert [1] .Green   =  0x00;
			vert [1] .Blue    =  0x00;
			vert [1] .Alpha   =  0xff00;

			u=(v-step2)*3.14159265358/180;
			vert [2] .x       =  cos(u)*r2;
			vert [2] .y       =  sin(u)*r2;
			vert [2] .Red     =  0x00;
			vert [2] .Green   =  0x00;
			vert [2] .Blue    =  0x00;
			vert [2] .Alpha   =  0xff00;
			GradientFill(hBackBufferDC,vert,3,&gTri,1,GRADIENT_FILL_TRIANGLE);
		}
		angle+=120;
	}
	for(v=angle+step;v<=angle+360;v+=step)
	{
		u=v*3.14159265358/180;
		vert [0] .x       =  0;
		vert [0] .y       =  0;
		vert [0] .Red     =  0x00;
		vert [0] .Green   =  0x00;
		vert [0] .Blue    =  0x00;
		vert [0] .Alpha   =  0xff00;

		vert [1] .x       =  cos(u)*r0;
		vert [1] .y       =  sin(u)*r0;
		vert [1] .Red     =  0x00;
		vert [1] .Green   =  0x00;
		vert [1] .Blue    =  0x00;
		vert [1] .Alpha   =  0xff00;

		u=(v-step)*3.14159265358/180;
		vert [2] .x       =  cos(u)*r0;
		vert [2] .y       =  sin(u)*r0;
		vert [2] .Red     =  0x00;
		vert [2] .Green   =  0x00;
		vert [2] .Blue    =  0x00;
		vert [2] .Alpha   =  0xff00;
		GradientFill(hBackBufferDC,vert,3,&gTri,1,GRADIENT_FILL_TRIANGLE);
	}
}

void CMainWindow::DrawAlarmLight(float angle, float halfwidth)
{
	TRIVERTEX        vert [3] ;
	GRADIENT_TRIANGLE    gTri[1];
	float step=2*halfwidth/10;
	float v;
	gTri[0].Vertex1   = 0;
	gTri[0].Vertex2   = 1;
	gTri[0].Vertex3   = 2;
	for(v=angle-halfwidth;v<=angle+halfwidth-step;v+=step)
	{
		vert [0] .x       =  0;
		vert [0] .y       =  0;
		vert [0] .Red     =  0xff00;
		vert [0] .Green   =  0x0000;
		vert [0] .Blue    =  0x0000;
		vert [0] .Alpha   =  0x4000;

		vert [1] .x       =  cos(v)*64;
		vert [1] .y       =  sin(v)*64;
		vert [1] .Red     =  0x00;
		vert [1] .Green   =  0x00;
		vert [1] .Blue    =  0x00;
		vert [1] .Alpha   =  0x00;

		vert [2] .x       =  cos(v+step)*64;
		vert [2] .y       =  sin(v+step)*64;
		vert [2] .Red     =  0x00;
		vert [2] .Green   =  0x00;
		vert [2] .Blue    =  0x00;
		vert [2] .Alpha   =  0x00;
		GradientFill(hBackBufferDC,vert,3,&gTri,1,GRADIENT_FILL_TRIANGLE);
	}
	//angle+=3.14159265358*2/3.0;
	angle+=3.14159265358;
	for(v=angle-halfwidth;v<=angle+halfwidth-step;v+=step)
	{
		vert [0] .x       =  0;
		vert [0] .y       =  0;
		vert [0] .Red     =  0x000;
		vert [0] .Green   =  0x000;
		vert [0] .Blue    =  0xff00;
		vert [0] .Alpha   =  0x4000;

		vert [1] .x       =  cos(v)*64;
		vert [1] .y       =  sin(v)*64;
		vert [1] .Red     =  0x00;
		vert [1] .Green   =  0x00;
		vert [1] .Blue    =  0x00;
		vert [1] .Alpha   =  0x00;

		vert [2] .x       =  cos(v+step)*64;
		vert [2] .y       =  sin(v+step)*64;
		vert [2] .Red     =  0x00;
		vert [2] .Green   =  0x00;
		vert [2] .Blue    =  0x00;
		vert [2] .Alpha   =  0x00;
		GradientFill(hBackBufferDC,vert,3,&gTri,1,GRADIENT_FILL_TRIANGLE);
	}
	/*angle+=3.14159265358*2/3.0;
	for(v=angle-halfwidth;v<=angle+halfwidth-step;v+=step)
	{
		vert [0] .x       =  0+64;
		vert [0] .y       =  0+64;
		vert [0] .Red     =  0x0000;
		vert [0] .Green   =  0xff00;
		vert [0] .Blue    =  0x00;
		vert [0] .Alpha   =  0x4000;

		vert [1] .x       =  cos(v)*64+64;
		vert [1] .y       =  sin(v)*64+64;
		vert [1] .Red     =  0x00;
		vert [1] .Green   =  0x00;
		vert [1] .Blue    =  0x00;
		vert [1] .Alpha   =  0x00;

		vert [2] .x       =  cos(v+step)*64+64;
		vert [2] .y       =  sin(v+step)*64+64;
		vert [2] .Red     =  0x00;
		vert [2] .Green   =  0x00;
		vert [2] .Blue    =  0x00;
		vert [2] .Alpha   =  0x00;
		GradientFill(hBackBufferDC,vert,3,&gTri,1,GRADIENT_FILL_TRIANGLE);
	}*/
	
}
/*void CMainWindow::DrawRadiationAlarm(int angle)
{
	TRIVERTEX        vert [3] ;
	GRADIENT_TRIANGLE    gTri[1];
	int step=20;
	int v;
	int halfwidth=30;
	int step2=20;
	float r=64,r0=12,r1=16,r2=50;
	float u;
	float c=(Size.cx-1)/2.0;
	gTri[0].Vertex1   = 0;
	gTri[0].Vertex2   = 1;
	gTri[0].Vertex3   = 2;
	for(v=step;v<=360;v+=step)
	{
		u=v*3.14159265358/180;
		vert [0] .x       =  0+c;
		vert [0] .y       =  0+c;
		vert [0] .Red     =  0xff00;
		vert [0] .Green   =  0xff00;
		vert [0] .Blue    =  0x0000;
		vert [0] .Alpha   =  0x7f00;

		vert [1] .x       =  cos(u)*r+c;
		vert [1] .y       =  sin(u)*r+c;
		vert [1] .Red     =  0x00;
		vert [1] .Green   =  0x00;
		vert [1] .Blue    =  0x00;
		vert [1] .Alpha   =  0x00;

		u=(v-step)*3.14159265358/180;
		vert [2] .x       =  cos(u)*r+c;
		vert [2] .y       =  sin(u)*r+c;
		vert [2] .Red     =  0x00;
		vert [2] .Green   =  0x00;
		vert [2] .Blue    =  0x00;
		vert [2] .Alpha   =  0x00;
		GradientFill(hBackBufferDC,vert,3,&gTri,1,GRADIENT_FILL_TRIANGLE);
	}
	for(int i=0;i<3;i++)
	{
		for(v=angle-halfwidth+step2;v<=angle+halfwidth;v+=step2)
		{
			u=v*3.14159265358/180;
			vert [0] .x       =  cos(u)*r1+c;
			vert [0] .y       =  sin(u)*r1+c;
			vert [0] .Red     =  0x000;
			vert [0] .Green   =  0x000;
			vert [0] .Blue    =  0x000;
			vert [0] .Alpha   =  0xff00;

			vert [1] .x       =  cos(u)*r2+c;
			vert [1] .y       =  sin(u)*r2+c;
			vert [1] .Red     =  0x00;
			vert [1] .Green   =  0x00;
			vert [1] .Blue    =  0x00;
			vert [1] .Alpha   =  0xff00;

			u=(v-step2)*3.14159265358/180;
			vert [2] .x       =  cos(u)*r2+c;
			vert [2] .y       =  sin(u)*r2+c;
			vert [2] .Red     =  0x00;
			vert [2] .Green   =  0x00;
			vert [2] .Blue    =  0x00;
			vert [2] .Alpha   =  0xff00;
			GradientFill(hBackBufferDC,vert,3,&gTri,1,GRADIENT_FILL_TRIANGLE);
			u=(v-step2)*3.14159265358/180;
			vert [0] .x       =  cos(u)*r1+c;
			vert [0] .y       =  sin(u)*r1+c;
			vert [0] .Red     =  0x000;
			vert [0] .Green   =  0x000;
			vert [0] .Blue    =  0x000;
			vert [0] .Alpha   =  0xff00;

			u=v*3.14159265358/180;
			vert [1] .x       =  cos(u)*r1+c;
			vert [1] .y       =  sin(u)*r1+c;
			vert [1] .Red     =  0x00;
			vert [1] .Green   =  0x00;
			vert [1] .Blue    =  0x00;
			vert [1] .Alpha   =  0xff00;

			u=(v-step2)*3.14159265358/180;
			vert [2] .x       =  cos(u)*r2+c;
			vert [2] .y       =  sin(u)*r2+c;
			vert [2] .Red     =  0x00;
			vert [2] .Green   =  0x00;
			vert [2] .Blue    =  0x00;
			vert [2] .Alpha   =  0xff00;
			GradientFill(hBackBufferDC,vert,3,&gTri,1,GRADIENT_FILL_TRIANGLE);
		}
		angle+=120;
	}
	for(v=angle+step;v<=angle+360;v+=step)
	{
		u=v*3.14159265358/180;
		vert [0] .x       =  0+c;
		vert [0] .y       =  0+c;
		vert [0] .Red     =  0x00;
		vert [0] .Green   =  0x00;
		vert [0] .Blue    =  0x00;
		vert [0] .Alpha   =  0xff00;

		vert [1] .x       =  cos(u)*r0+c;
		vert [1] .y       =  sin(u)*r0+c;
		vert [1] .Red     =  0x00;
		vert [1] .Green   =  0x00;
		vert [1] .Blue    =  0x00;
		vert [1] .Alpha   =  0xff00;

		u=(v-step)*3.14159265358/180;
		vert [2] .x       =  cos(u)*r0+c;
		vert [2] .y       =  sin(u)*r0+c;
		vert [2] .Red     =  0x00;
		vert [2] .Green   =  0x00;
		vert [2] .Blue    =  0x00;
		vert [2] .Alpha   =  0xff00;
		GradientFill(hBackBufferDC,vert,3,&gTri,1,GRADIENT_FILL_TRIANGLE);
	}
}

void CMainWindow::DrawAlarmLight(float angle, float halfwidth)
{
	TRIVERTEX        vert [3] ;
	GRADIENT_TRIANGLE    gTri[1];
	float step=2*halfwidth/10;
	float v;
	gTri[0].Vertex1   = 0;
	gTri[0].Vertex2   = 1;
	gTri[0].Vertex3   = 2;
	for(v=angle-halfwidth;v<=angle+halfwidth-step;v+=step)
	{
		vert [0] .x       =  0+64;
		vert [0] .y       =  0+64;
		vert [0] .Red     =  0xff00;
		vert [0] .Green   =  0x0000;
		vert [0] .Blue    =  0x0000;
		vert [0] .Alpha   =  0x4000;

		vert [1] .x       =  cos(v)*64+64;
		vert [1] .y       =  sin(v)*64+64;
		vert [1] .Red     =  0x00;
		vert [1] .Green   =  0x00;
		vert [1] .Blue    =  0x00;
		vert [1] .Alpha   =  0x00;

		vert [2] .x       =  cos(v+step)*64+64;
		vert [2] .y       =  sin(v+step)*64+64;
		vert [2] .Red     =  0x00;
		vert [2] .Green   =  0x00;
		vert [2] .Blue    =  0x00;
		vert [2] .Alpha   =  0x00;
		GradientFill(hBackBufferDC,vert,3,&gTri,1,GRADIENT_FILL_TRIANGLE);
	}
	//angle+=3.14159265358*2/3.0;
	angle+=3.14159265358;
	for(v=angle-halfwidth;v<=angle+halfwidth-step;v+=step)
	{
		vert [0] .x       =  0+64;
		vert [0] .y       =  0+64;
		vert [0] .Red     =  0x000;
		vert [0] .Green   =  0x000;
		vert [0] .Blue    =  0xff00;
		vert [0] .Alpha   =  0x4000;

		vert [1] .x       =  cos(v)*64+64;
		vert [1] .y       =  sin(v)*64+64;
		vert [1] .Red     =  0x00;
		vert [1] .Green   =  0x00;
		vert [1] .Blue    =  0x00;
		vert [1] .Alpha   =  0x00;

		vert [2] .x       =  cos(v+step)*64+64;
		vert [2] .y       =  sin(v+step)*64+64;
		vert [2] .Red     =  0x00;
		vert [2] .Green   =  0x00;
		vert [2] .Blue    =  0x00;
		vert [2] .Alpha   =  0x00;
		GradientFill(hBackBufferDC,vert,3,&gTri,1,GRADIENT_FILL_TRIANGLE);
	}
	//start angle+=3.14159265358*2/3.0;
	for(v=angle-halfwidth;v<=angle+halfwidth-step;v+=step)
	{
		vert [0] .x       =  0+64;
		vert [0] .y       =  0+64;
		vert [0] .Red     =  0x0000;
		vert [0] .Green   =  0xff00;
		vert [0] .Blue    =  0x00;
		vert [0] .Alpha   =  0x4000;

		vert [1] .x       =  cos(v)*64+64;
		vert [1] .y       =  sin(v)*64+64;
		vert [1] .Red     =  0x00;
		vert [1] .Green   =  0x00;
		vert [1] .Blue    =  0x00;
		vert [1] .Alpha   =  0x00;

		vert [2] .x       =  cos(v+step)*64+64;
		vert [2] .y       =  sin(v+step)*64+64;
		vert [2] .Red     =  0x00;
		vert [2] .Green   =  0x00;
		vert [2] .Blue    =  0x00;
		vert [2] .Alpha   =  0x00;
		GradientFill(hBackBufferDC,vert,3,&gTri,1,GRADIENT_FILL_TRIANGLE);
	//end}
	
}*/



void CMainWindow::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	if(!bMoveingWindow)
	{
		bMoveingWindow=true;
		LastMousePoint=point;
		this->SetCapture();
	}
	CWnd::OnLButtonDown(nFlags, point);
}

void CMainWindow::OnLButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	if(bMoveingWindow)
	{
		bMoveingWindow=false;
		::ReleaseCapture();
	}
	CWnd::OnLButtonUp(nFlags, point);
}

void CMainWindow::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	if(bMoveingWindow)
	{
		point-=LastMousePoint;
		ptDst.x+=point.x;
		ptDst.y+=point.y;
		::UpdateLayeredWindow(this->m_hWnd,hScreenDC,&ptDst,&Size,hBackBufferDC, &ptSrc,RGB(0xff,0xff,0x0),&Blend,ULW_ALPHA);
	}
	CWnd::OnMouseMove(nFlags, point);
}

BOOL CMainWindow::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt) 
{
	// TODO: Add your message handler code here and/or call default
	d+=zDelta;
	return CWnd::OnMouseWheel(nFlags, zDelta, pt);
}

void CMainWindow::OnLButtonDblClk(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	nMethod=(nMethod+1)%3;
	CWnd::OnLButtonDblClk(nFlags, point);
}

void CMainWindow::OnMoving(UINT fwSide, LPRECT pRect) 
{
	CWnd::OnMoving(fwSide, pRect);
	
	// TODO: Add your message handler code here
	
}

void CMainWindow::OnMove(int x, int y) 
{
	CWnd::OnMove(x, y);
	ptDst.x=x;
	ptDst.y=y;
	// TODO: Add your message handler code here
	
}

void CMainWindow::OnDropFiles(HDROP hDropInfo) 
{
	// TODO: Add your message handler code here and/or call default
	
	CWnd::OnDropFiles(hDropInfo);
}

void CMainWindow::OnDestroy() 
{
	CWnd::OnDestroy();
	KillTimer(IDT_REFRESH);
	CloseAVI();
	::ReleaseDC(this->m_hWnd,hScreenDC);
	SelectObject(hBackBufferDC, hOldBitmap);
	DeleteObject(hBackBufferBitmap);
	DeleteObject(hBackBufferDC);
	delete pAlpha;
	// TODO: Add your message handler code here
	
}

void CMainWindow::OnContextMenu(CWnd*, CPoint point)
{

	// CG: This block was added by the Pop-up Menu component
	{
		if (point.x == -1 && point.y == -1){
			//keystroke invocation
			CRect rect;
			GetClientRect(rect);
			ClientToScreen(rect);

			point = rect.TopLeft();
			point.Offset(5, 5);
		}

		CMenu menu;
		VERIFY(menu.LoadMenu(IDR_SETTINGSMENU));

		CMenu* pPopup = menu.GetSubMenu(0);
		ASSERT(pPopup != NULL);
		CWnd* pWndPopupOwner = this;

		while (pWndPopupOwner->GetStyle() & WS_CHILD)
			pWndPopupOwner = pWndPopupOwner->GetParent();

		pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y,
			pWndPopupOwner);
	}
}

void CMainWindow::OnMenuitemExit() 
{
	// TODO: Add your command handler code here
	DestroyWindow();
}

void CMainWindow::OnMenuitemMethod1() 
{
	// TODO: Add your command handler code here
	this->nMethod=0;
}

void CMainWindow::OnMenuitemMethod2() 
{
	// TODO: Add your command handler code here
	this->nMethod=1;
}

void CMainWindow::OnMenuitemMethod3() 
{
	// TODO: Add your command handler code here
	this->nMethod=2;
	
}

void CMainWindow::OnNcDestroy() 
{
	CWnd::OnNcDestroy();
	// TODO: Add your message handler code here
	delete this;
}
void CMainWindow::flipIt(void* buffer)										// Flips The Red And Blue Bytes (256x256)
{
	void* b = buffer;											// Pointer To The Buffer
	__asm														// Assembler Code To Follow
	{
		mov ecx, 128*128										// Counter Set To Dimensions Of Our Memory Block
		mov ebx, b												// Points ebx To Our Data (b)
		label:													// Label Used For Looping
			mov al,[ebx+0]										// Loads Value At ebx Into al
			mov ah,[ebx+2]										// Loads Value At ebx+2 Into ah
			mov [ebx+2],al										// Stores Value In al At ebx+2
			mov [ebx+0],ah										// Stores Value In ah At ebx
			
			add ebx,4											// Moves Through The Data By 3 Bytes
			dec ecx												// Decreases Our Loop Counter
			jnz label											// If Not Zero Jump Back To Label
	}
}

BOOL CMainWindow::OpenAVI(LPCSTR szFile,LPCSTR szAlphaFile)										// Opens An AVI File (szFile)
{
	TCHAR	title[100];											// Will Hold The Modified Window Title

	AVIFileInit();												// Opens The AVIFile Library
	hdd = DrawDibOpen();		
	// Opens The AVI Stream
	if (AVIStreamOpenFromFile(&pavi, szFile, streamtypeVIDEO, 0, OF_READ, NULL) !=0)
	{
		// An Error Occurred Opening The Stream
		::MessageBox (NULL, "Failed To Open The AVI Stream", "Error", MB_OK | MB_ICONEXCLAMATION);
		AVIFileExit();
		return FALSE;
	}
	
	if (AVIStreamOpenFromFile(&paavi, szAlphaFile, streamtypeVIDEO, 0, OF_READ, NULL) !=0)
	{
		// An Error Occurred Opening The Stream
		::MessageBox (NULL, "Failed To Open The Alpha AVI Stream", "Error", MB_OK | MB_ICONEXCLAMATION);
		AVIFileExit();
		return FALSE;
	}

	AVIStreamInfo(pavi, &psi, sizeof(psi));						// Reads Information About The Stream Into psi
	AVIStreamInfo(paavi, &pasi, sizeof(pasi));						// Reads Information About The Stream Into psi
	width=psi.rcFrame.right-psi.rcFrame.left;					// Width Is Right Side Of Frame Minus Left
	height=psi.rcFrame.bottom-psi.rcFrame.top;					// Height Is Bottom Of Frame Minus Top

	lastframe=AVIStreamLength(pavi);							// The Last Frame Of The Stream

	//mpf=AVIStreamSampleToTime(pavi,lastframe)/lastframe;		// Calculate Rough Milliseconds Per Frame

	pgf=AVIStreamGetFrameOpen(pavi, NULL);						// Create The PGETFRAME	Using Our Request Mode
	if (pgf==NULL)
	{
		// An Error Occurred Opening The Frame
		::MessageBox (NULL, "Failed To Open The AVI Frame", "Error", MB_OK | MB_ICONEXCLAMATION);
		AVIFileExit(); 
		return FALSE;
	}
	pagf=AVIStreamGetFrameOpen(paavi, NULL);						// Create The PGETFRAME	Using Our Request Mode
	if (pgf==NULL)
	{
		// An Error Occurred Opening The Frame
		::MessageBox (NULL, "Failed To Open The Alpha AVI Frame", "Error", MB_OK | MB_ICONEXCLAMATION);
		AVIFileExit(); 
		return FALSE;
	}
	return TRUE;
}

void CMainWindow::GrabAVIFrame(int frame)									// Grabs A Frame From The Stream
{
	LPBITMAPINFOHEADER lpbi;									// Holds The Bitmap Header Information
	lpbi = (LPBITMAPINFOHEADER)AVIStreamGetFrame(pgf, frame);	// Grab Data From The AVI Stream
	pdata=(char *)lpbi+lpbi->biSize+lpbi->biClrUsed * sizeof(RGBQUAD);	// Pointer To Data Returned By AVIStreamGetFrame

	LPBITMAPINFOHEADER lpabi;									// Holds The Bitmap Header Information
	lpabi = (LPBITMAPINFOHEADER)AVIStreamGetFrame(pagf, frame);	// Grab Data From The AVI Stream
	padata=(char *)lpabi+lpabi->biSize+lpabi->biClrUsed * sizeof(RGBQUAD);	// Pointer To Data Returned By AVIStreamGetFrame

	// Convert Data To Requested Bitmap Format
	//DrawDibDraw (hdd, hBackBufferDC, -64, -64, 63, 63, lpbi, pdata, 0, 0, width, height, 0);
	PBYTE pTemp;
	pTemp=pBits;
	void *p1=pBits;
	void *p2=pdata;
	void *p3=padata;
	__asm
	{
		mov ecx, 128*128
		mov edi, p1
		mov esi, p2	
		mov ebx, p3	
		label:													
			mov eax,[esi]
			mov [edi],eax
			mov al,[ebx]
			mov [edi+3],al
			add edi,4
			add esi,3
			add ebx,3
			dec ecx									
			jnz label	
	}
	/*for(int k=0;k<128*128;k++)
	{
		*(pTemp++)=*(pdata++);
		*(pTemp++)=*(pdata++);
		*(pTemp++)=*(pdata++);
		*(pTemp++)=*padata;
		padata+=3;
	}*/

	//flipIt(pBits);												// Swap The Red And Blue Bytes (GL Compatability)
}

void CMainWindow::CloseAVI(void)				// Properly Closes The Avi File
{
	DrawDibClose(hdd);							// Closes The DrawDib Device Context
	AVIStreamGetFrameClose(pgf);				// Deallocates The GetFrame Resources
	AVIStreamRelease(pavi);						// Release The Stream
	AVIStreamGetFrameClose(pagf);				// Deallocates The GetFrame Resources
	AVIStreamRelease(paavi);						// Release The Stream
	AVIFileExit();								// Release The File
}

void CMainWindow::OnMenuitemMethod4() 
{
	// TODO: Add your command handler code here
	this->nMethod=3;
}

void CMainWindow::OnMenuitemAbout() 
{
	// TODO: Add your command handler code here
	MessageBox(_T("Athor: Zhou, Bo\nEmail:zhoubo61@msn.com"));
}
